<template>
  <div id="survey-container">
    <!-- <router-view/> -->
    <h1>{{title}}</h1>
    <div>
      <div v-for="s in list" :key="s.id">
        <h3>{{s.question}}</h3>
        <p>
          <el-input
            type="textarea"
            :rows="2"
            v-model="s.result"
            placeholder="请输入内容">

          </el-input>
        </p>
      </div>

      <div style="text-align: center"><el-button @click="handlePostResult" type="primary">提交</el-button></div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'App',
  data () {
    return {
      survey_id: this.$route.params['survey_id'],
      list: [],
      title: '',
      survey: null,
      loading: null
    }
  },
  methods: {
    handlePostResult () {
      this.createLoading()
      const json = this.list.map(it => {
        return {
          surveyId: it.survey_id[0],
          uid: 2,
          querstionId: it.id,
          result: it.result,
          answer_type: it.type
        }
      })
      console.log(json[0])
      const opt = {
        method: 'POST',
        body: JSON.stringify(json),
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        }
      }
      fetch('http://10.65.37.74:8080/survey/createInputLine', opt)
        .then(resp => {
          this.closeLoading()
          // this.$route.push({name: 'survey_list'})
        })
    },
    createLoading () {
      this.loading = this.$loading({lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
    },
    closeLoading () {
      this.loading.close()
    }
  },
  mounted () {
    this.createLoading()
    fetch(`http://10.65.37.74:8080/survey/queryQuestionsAll?surveyId=${this.survey_id}`)
      .then(rsp => {
        this.closeLoading()
        rsp.json().then(data => {
          this.title = data[0][0].display_name
          this.survey = data[0][0]

          data[1].map(it => {
            it.result = ''
            it.uid = 2
          })
          this.list = data[1]
          // console.log(this.survey)
          // console.log(data[1])
        })
      })
  }
}
</script>

<style>
#survey-container{
  width: 70%;
  margin: 0 auto;
  text-align: left;
}
</style>
